---
title: "Correlation"
author: "`r Sys.info()['user']`"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 4)
require(gplots)
require(LSX)

dat_en <- cbind(readRDS("data_lss_en.RDS"), readRDS("data_lsd_en.RDS"))
dat_ja <- cbind(readRDS("data_lss_ja.RDS"))
```

## Economy

```{r, fig.width = 8, fig.height = 4}
par(mfrow = c(1, 2), font.main = 1)
par(mar = c(4, 4, 2, 1))
plotmeans(lsd ~ human, dat_en, ylim = c(-1, 1.5), main = "LSD", xlab = "Human", ylab = "Machine")
plotmeans(lss ~ human, dat_en, ylim = c(-1, 1.5), main = "LSS", xlab = "Human", ylab = "Machine")
```

```{r}
par(mar = c(2, 4, 1, 4))
agg_en_year <- aggregate(list(lss = dat_en$lss, 
                           lsd = dat_en$lsd, 
                           human = dat_en$human), 
                      by = list(year = dat_en$year), FUN = mean)

plot(agg_en_year$year, agg_en_year$human, ylim = c(-1.5, 1),
     type = "b", ylab = "Sentiment", pch = 1)
lines(agg_en_year$year, agg_en_year$lss, col = "red", type = "b", pch = 2)
lines(agg_en_year$year, agg_en_year$lsd, col = "blue", type = "b", pch = 3)
grid()
legend("topleft", c("Human", "LSS", "LSD"), col = c("black", "red", "blue"), 
       horiz = TRUE, pch = 1:3)
par(new = TRUE)
plot(table(dat_en$year), ylim = c(0, 200), type = "h",
     xaxt = "n", yaxt = "n", xlab = "", ylab = "", lend = 2, lwd = 2)
axis(4, seq(0, 40, 10))
```

```{r, fig.width = 8, fig.height = 4}
par(mfrow = c(1, 2), font.main = 1)
par(mar = c(4, 4, 2, 1))
plot(agg_en_year$human, agg_en_year$lsd, xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5),
     main = "LSD", xlab = "Human", ylab = "Machine")
grid()
abline(lm(agg_en_year$lsd ~ agg_en_year$human), lty = 3)
plot(agg_en_year$human, agg_en_year$lss, xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5),
     main = "LSS", xlab = "Human", ylab = "Machine")
grid()
abline(lm(agg_en_year$lss ~ agg_en_year$human), lty = 3)
```

```{r}
cor(agg_en_year$lsd, agg_en_year$human)
cor(agg_en_year$lss, agg_en_year$human)
cor(agg_en_year$lss, agg_en_year$lsd)
```


```{r}
par(mar = c(2, 4, 1, 4))
dat_gdp <- readRDS("data_gdp.RDS")
plot(dat_gdp$year, dat_gdp$lss, type = "b", ylim = c(-5, 5), col = "red", pch = 2,
     ylab = "Sentiment")
lines(dat_gdp$year, dat_gdp$lsd, type = "b", col = "blue", pch = 3)
grid()
legend("topright", c("GDP", "LSS", "LSD"), col = c("black", "red", "blue"), 
       horiz = TRUE, pch = 1:3)
par(new = TRUE)
plot(dat_gdp$year, dat_gdp$gdp_growth, ylim = c(-3, 8), type = "b",
     xaxt = "n", yaxt = "n", xlab = "", ylab = "", pch = 1)
axis(4, seq(-3, 8, 2))
mtext("US GDP Growth (%)", side = 4, line = 3)
```

```{r}
cor(dat_gdp$lss, dat_gdp$gdp_growth)
cor(dat_gdp$lsd, dat_gdp$gdp_growth)
cor(dat_gdp$lsd, dat_gdp$lss)
```

## Politics

```{r}
toks_mnu <- readRDS("data_tokens_manual_ja.RDS")
dat_code <- docvars(toks_mnu, c("code_1", "code_2", "code_3"))
irr::kripp.alpha(t(as.matrix(dat_code)), "ordinal")
```

```{r, fig.width = 4, fig.height = 4}
par(mfrow = c(1, 1), font.main = 1)
par(mar = c(4, 4, 2, 1))
plotmeans(lss ~ human, dat_ja, ylim = c(-1, 1.5), main = "LSS", xlab = "Human", ylab = "Machine")
par(mfrow = c(1, 1))
```

```{r}
par(mar = c(2, 4, 1, 4))
agg_en_year <- aggregate(list(lss = dat_ja$lss, 
                           human = dat_ja$human), 
                      by = list(year = dat_ja$year), FUN = mean)

plot(agg_en_year$year, agg_en_year$human, ylim = c(-1.5, 0.5),
     type = "b", ylab = "Sentiment", pch = 1)
lines(agg_en_year$year, agg_en_year$lss, col = "red", type = "b", pch = 2)
grid()
legend("topleft", c("Human", "LSS"), col = c("black", "red", "blue"), 
       horiz = TRUE, pch = 1:3)
par(new = TRUE)
plot(table(dat_ja$year), ylim = c(0, 200), type = "h",
     xaxt = "n", yaxt = "n", xlab = "", ylab = "", lend = 2, lwd = 2)
axis(4, seq(0, 40, 10))
```

```{r, fig.width = 4, fig.height = 4}
par(mfrow = c(1, 1), font.main = 1)
par(mar = c(4, 4, 2, 1))
plot(agg_en_year$human, agg_en_year$lss, xlim = c(-1, 1), ylim = c(-1, 1),
     main = "LSS", xlab = "Human", ylab = "Machine")
grid()
abline(lm(agg_en_year$lss ~ agg_en_year$human), lty = 3)
```

```{r}
cor(agg_en_year$lss, agg_en_year$human)
```


